#include "UserInfoForm.h"
#include <QFileDialog>
UserInfoForm::UserInfoForm(QWidget* parent)
    : QDialog(parent)
{
    ui.setupUi(this);

    connect(ui.pushButton_save, &QPushButton::clicked, this, &UserInfoForm::on_buttonSave_clicked);
    connect(ui.pushButton_cacel, &QPushButton::clicked, this, &UserInfoForm::on_buttonCancel_clicked);
    connect(ui.pushButton_sel, &QPushButton::clicked, this, &UserInfoForm::on_buttonSel_clicked);
    connect(ui.pushButton_showHide, &QPushButton::clicked, this, &UserInfoForm::on_buttonShowHide_clicked);

}

UserInfoForm::~UserInfoForm()
{}

void UserInfoForm::setUserInfo(const UserInfo & userinfo)
{
    m_userInfo = userinfo;
    updateUI();
}

UserInfo UserInfoForm::GetUserInfo()
{
    return m_userInfo;
}

void UserInfoForm::updateUI()
{
    ui.lineEdit_userName->setText(m_userInfo.name());
    ui.lineEdit_passWord->setText(m_userInfo.password());
    ui.lineEdit_IconPath->setText(m_userInfo.iconPath());
    ui.comboBox_type->setCurrentText(m_userInfo.OperatorLevel());
}

void UserInfoForm::SaveInfo()
{
    m_userInfo.setName(ui.lineEdit_userName->text());
    m_userInfo.setPassWord(ui.lineEdit_passWord->text());
    m_userInfo.setIconPath(ui.lineEdit_IconPath->text());
    m_userInfo.SetOperatorLevel(ui.comboBox_type->currentText());

}

void UserInfoForm::on_buttonSave_clicked()
{
    SaveInfo();
    accept();
}

void UserInfoForm::on_buttonCancel_clicked()
{
    reject();
}

void UserInfoForm::on_buttonSel_clicked()
{
    QString file = QFileDialog::getOpenFileName(nullptr,
        tr("选择 图标 文件"),
        "",
        tr("图标文件 (*.png)"));

    if (file!=NULL)
    {
        ui.lineEdit_IconPath->setText(file);
    }
}

void UserInfoForm::on_buttonShowHide_clicked()
{
    if (ui.pushButton_showHide->text() == QString::fromLocal8Bit("显示"))
    {
        ui.pushButton_showHide->setText(QString::fromLocal8Bit("隐藏"));
        ui.lineEdit_passWord->setEchoMode(QLineEdit::Normal);
        return;
    }

    if (ui.pushButton_showHide->text() == QString::fromLocal8Bit("隐藏"))
    {
        ui.pushButton_showHide->setText(QString::fromLocal8Bit("显示"));
        ui.lineEdit_passWord->setEchoMode(QLineEdit::Password);
        return;
    }
}
